# -*- coding: utf-8 -*-

from odoo.http import request

from marshmallow import Schema, fields as orig_fields
from odoo_contrib.marshmallow import fields


class EnabledExhibitIdField(fields.Integer):
    def _serialize(self, value, attr, obj, **kwargs):
        if not obj.exhibit_id or not obj.exhibit_id.enabled:
            value = False
        return super()._serialize(value, attr, obj, **kwargs)


class EnabledExhibitNameField(fields.Str):
    def _serialize(self, value, attr, obj, **kwargs):
        if not obj.exhibit_id or not obj.exhibit_id.enabled:
            value = False
        return super()._serialize(value, attr, obj, **kwargs)


class EnabledArticleIdField(fields.Integer):
    def _serialize(self, value, attr, obj, **kwargs):
        if not obj.article_id or not obj.article_id.enabled:
            value = False
        return super()._serialize(value, attr, obj, **kwargs)


class EnabledArticleTitleField(fields.Str):
    def _serialize(self, value, attr, obj, **kwargs):
        if not obj.article_id or not obj.article_id.enabled:
            value = False
        return super()._serialize(value, attr, obj, **kwargs)


class CarouselSchema(Schema):
    id_ = fields.Integer(attribute='id', data_key='id')
    image_url = fields.AttachmentUrl(attribute='image_id', data_key='imageUrl')
    article_id = EnabledArticleIdField(data_key='articleId')
    article_title = EnabledArticleTitleField(data_key='articleTitle')
    exhibit_id = EnabledExhibitIdField(data_key='exhibitId')
    exhibit_name = EnabledExhibitNameField(data_key='exhibitName')


class CarouselRepositorySchema(Schema):
    entries = fields.List(fields.Nested(CarouselSchema))
    count = fields.Integer()
